package com.dataaccess.dataaccess.web.service;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.dataaccess.dataaccess.common.base.BaseService;
import com.dataaccess.dataaccess.model.BusinessCheckaddrType;
import com.dataaccess.dataaccess.model.BusinessType;
import com.dataaccess.dataaccess.support.model.RespEntity;
import com.dataaccess.dataaccess.web.mapper.BusinessCheckaddrTypeMapper;
import com.dataaccess.dataaccess.web.mapper.FireDepartmentMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Yang
 */
@Service
public class BulkInsertService extends BaseService {
    @Resource
    private FireDepartmentMapper fireDepartmentMapper;
	@Resource
	private BusinessCheckaddrTypeMapper businessCheckaddrTypeMapper;

    /**
     *
     * @param params
     * @return
     * @throws Exception
     */
    public RespEntity addUpload2(JSONArray params) throws Exception{


        List<BusinessCheckaddrType> businessCheckaddrTypeList = new ArrayList<>();
		l:for(Object one :params) {
			JSONObject oneJsonParam = (JSONObject)one;
			if(oneJsonParam.getStr("name").equals("全部")) continue l;
			BusinessCheckaddrType oneType = new BusinessCheckaddrType();
			oneType.setId(oneJsonParam.getStr("id"));
			oneType.setLevel(1);
			oneType.setName(oneJsonParam.getStr("name"));
			oneType.setPid("0");
			JSONArray oneJsonArray  = oneJsonParam.getJSONArray("children");
			if(!oneJsonArray.isEmpty()){
				m:for(Object two :oneJsonArray) {
					JSONObject twoJsonParam = (JSONObject)two;
					if(twoJsonParam.getStr("name").equals("全部")) continue m;
					BusinessCheckaddrType twoType = new BusinessCheckaddrType();
					twoType.setId(twoJsonParam.getStr("id"));
					twoType.setLevel(2);
					twoType.setName(twoJsonParam.getStr("name"));
					twoType.setPid(oneJsonParam.getStr("id"));
					JSONArray twoJsonArray  = twoJsonParam.getJSONArray("children");
					if(!twoJsonArray.isEmpty()){
						n:for(Object three :twoJsonArray) {
							JSONObject threeJsonParam = (JSONObject)three;
							if(threeJsonParam.getStr("name").equals("全部")) continue n;
							BusinessCheckaddrType threeType = new BusinessCheckaddrType();
							threeType.setId(threeJsonParam.getStr("id"));
							threeType.setLevel(3);
							threeType.setName(threeJsonParam.getStr("name"));
							threeType.setPid(twoJsonParam.getStr("id"));
//							JSONArray twoJsonArray  = twoJsonParam.getJSONArray("children");
//							if(!twoJsonArray.isEmpty()){
//
//							}
							businessCheckaddrTypeList.add(threeType);
						}
					}
					businessCheckaddrTypeList.add(twoType);
				}
			}
			businessCheckaddrTypeList.add(oneType);
		}
		//businessCheckaddrTypeMapper.insertZB(businessCheckaddrTypeList);
        return RespEntity.success();
    }
	public RespEntity addUpload(JSONArray params) throws Exception{
		List<BusinessType> businessTypeList = new ArrayList<>();
		BusinessType businessType = new BusinessType();
		for (Object type : params){
			businessType = new BusinessType();
			JSONObject typeParam = (JSONObject)type;
			businessType.setId(typeParam.getStr("BH"));
			String[] ss = typeParam.getStr("MC").split("-");
			businessType.setName(ss[ss.length-1]);
//			businessType.setPid(typeParam.getStr("ParID"));
//			businessType.setLevel(typeParam.getInt("lvl"));
			businessTypeList.add(businessType);
		}
		businessCheckaddrTypeMapper.insertZB(businessTypeList);
		return RespEntity.success(businessTypeList);
	}
}


